package com.yu.interceptor;

import com.yu.comfig.SpringBeanUtil;
import com.yu.entity.AuthHost;
import com.yu.mapper.AuthHostMapper;
import com.yu.mapper.AuthUserMapper;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.transport.http.AbstractHTTPDestination;

import javax.servlet.http.HttpServletRequest;

/**
 * @author : wenchao.long
 * @date : Created in 2021/01/05 11:23 周二
 * @description :
 */
public class AuthInterceptor extends AbstractSoapInterceptor {

    private static final String BASIC_PREFIX = "Basic ";

    private AuthUserMapper authUserMapper;

    private AuthHostMapper authHostMapper;

    public AuthInterceptor() {
        super(Phase.PRE_PROTOCOL);
    }

    public AuthInterceptor(String phase) {
        super(phase);
    }

    public AuthInterceptor(String id, String phase) {
        super(id, phase);
    }

    @Override
    public void handleMessage(SoapMessage message) throws Fault {
        if (authUserMapper == null) {
            authUserMapper = (AuthUserMapper) SpringBeanUtil.getBean("authUserMapper");
        }
        if (authHostMapper == null) {
            authHostMapper = (AuthHostMapper) SpringBeanUtil.getBean("authHostMapper");
        }
        HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
        // 认证远程主机是否是授权主机
        AuthHost authHost = new AuthHost();
        authHost.setHost(request.getRemoteHost());
//        List<AuthHost> authHosts = authHostMapper.selectAuthHost(authHost);
//        if (authHosts.size() < 1) {
//            throw new Fault(new SOAPException("auth failed, Remote host and non-privileged host"));
//        }

        // 认证是否添加权限
//        String auth = request.getHeader("Authorization");
//        if (auth == null) {
//            throw new Fault(new SOAPException("auth failed, header [Authorization] not exists"));
//        }

        // 认证模式是否正确
//        if (!auth.startsWith(BASIC_PREFIX)) {
//            throw new Fault(new SOAPException("auth failed, header [Authorization] is illegal"));
//        }

        // 认证是否添加用户名与密码
//        String plainText = new String(Base64.getDecoder().decode(auth.substring(BASIC_PREFIX.length())));
//        if (StringUtils.isEmpty(plainText) || !plainText.contains(":")) {
//            throw new Fault(new SOAPException("auth failed, header [Authorization] is illegal"));
//        }
//        String[] userAndPass = plainText.split(":");
//        if (userAndPass.length < 2) {
//            throw new Fault(new SOAPException("auth failed, username is null or password is null"));
//        }

        // 认证用户名与密码是否正确
//        AuthUser authUser = new AuthUser();
//        authUser.setUserName(userAndPass[0]);
//        authUser.setPassword(userAndPass[1]);
//        List<AuthUser> authUsers = authUserMapper.selectAuthUser(authUser);
//        if (authUsers.size() < 1) {
//            throw new Fault(new SOAPException("auth failed, username or password error"));
//        }
//        request.setAttribute("userName", authUser);

    }
}
